package gold.digger;

import gold.utils.InputUtil;
import gold.utils.UF;

import java.util.ArrayList;
import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class LC2101 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        // 炸弹引爆最大数量
        int max = 1;
        // 炸弹是否引爆备忘录
        boolean[] mem;

        public int maximumDetonation(int[][] bombs) {
            mem = new boolean[bombs.length];
            for (int i = 0; i < bombs.length; i++) {
                // 先引爆一个
                dfs(bombs, i);
                // 所有炸弹置为未引爆状态
                Arrays.fill(mem, false);
            }
            return max;
        }

        private int dfs(int[][] bombs, int i) {
            if (mem[i]) {
                return 0;
            }
            mem[i] = true;
            int ret = 1;
            // 遍历所有炸弹，判断是否会连带引爆
            for (int j = 0; j < bombs.length; j++) {
                if (mem[j]) continue;
                if (canBom(bombs, i, j)) {
                    // j爆炸之后，会带动其他的炸弹爆炸，继续dfs
                    ret += dfs(bombs, j);
                }
            }
            max = Math.max(max, ret);
            return ret;
        }

        private boolean canBom(int[][] bombs, int i, int j) {
            int[] b1 = bombs[i];
            int[] b2 = bombs[j];
            long x0 = b1[0], x1 = b2[0], y0 = b1[1], y1 = b2[1], r0 = b1[2];
            long len = (y1 - y0) * (y1 - y0) + (x1 - x0) * (x1 - x0);
            long r02 = r0 * r0;
            // 【两点距离的平方】(y1-y0)^2 + (x1-x0)^2 < 【引爆半径的平方】r0^2 则会被引爆
            if (len <= r02) return true;

            return false;

        }

    }


    class Solution_Wrong_Thought {

        /**
         * Created by fanzhenyu02 on 2020/6/27.
         * Union Find 算法实现
         */
        public class UF {
            // 连通分量个数
            private int count;
            // 存储一棵树
            private int[] parent;
            // 记录树的“重量”
            private int[] size;

            public UF(int n) {
                this.count = n;
                parent = new int[n];
                size = new int[n];
                for (int i = 0; i < n; i++) {
                    parent[i] = i;
                    size[i] = 1;
                }
            }

            /* 将 p 和 q 连接 */
            public void union(int p, int q) {
                int rootP = find(p);
                int rootQ = find(q);
                if (rootP == rootQ)
                    return;

                // 小树接到大树下面，较平衡
                if (size[rootP] > size[rootQ]) {
                    parent[rootQ] = rootP;
                    size[rootP] += size[rootQ];
                } else {
                    parent[rootP] = rootQ;
                    size[rootQ] += size[rootP];
                }
                count--;
            }

            /* 判断 p 和 q 是否连通 */
            public boolean connected(int p, int q) {
                int rootP = find(p);
                int rootQ = find(q);
                return rootP == rootQ;
            }

            private int find(int x) {
                while (parent[x] != x) {
                    // 进行路径压缩
                    parent[x] = parent[parent[x]];
                    x = parent[x];
                }
                return x;
            }

            /* 返回图中有多少个连通分量 */
            public int count() {
                return count;
            }

            /* 返回图中联通集合 */
            public Map<Integer, List<Integer>> outputAggregateSet(int n) {
                Map<Integer, List<Integer>> aggregateMap = new HashMap<>();

                // 共有n个集合
                for (int i = 0; i < n; i++) {
                    int parentId = find(i);
                    if (!aggregateMap.containsKey(parentId)) aggregateMap.put(parentId, new ArrayList<>());
                    aggregateMap.get(parentId).add(i);
                }

                System.out.println(aggregateMap.toString());
                return aggregateMap;
            }
        }

        public int maximumDetonation(int[][] bombs) {
            int n = bombs.length;
            UF uf = new UF(n);
            for (int i = 0; i < n; i++) {
                for (int j = i + 1; j < n; j++) {
                    if (isConn(bombs, i, j)) {
                        uf.union(i, j);
                    }
                }
            }

            int ans = 0;
            for (int i = 0; i < n; i++) {
                if (uf.size[i] > ans) ans = uf.size[i];
            }

            return ans;
        }

        public boolean isConn(int[][] bombs, int i, int j) {
            long dis = 1L * (bombs[i][0] - bombs[j][0]) * (bombs[i][0] - bombs[j][0]) + 1L * (bombs[i][1] - bombs[j][1]) * (bombs[i][1] - bombs[j][1]);
            long detonateRadius = Math.max(bombs[i][2], bombs[j][2]);
            detonateRadius = detonateRadius * detonateRadius;
            return detonateRadius >= dis;
        }
    }

    public void run() {
//        System.out.println(new Solution().maximumDetonation(InputUtil.toDoubleIntegerArray("[[2,1,3],[6,1,4]]")));
//        System.out.println(new Solution().maximumDetonation(InputUtil.toDoubleIntegerArray("[[1,1,5],[10,10,5]]")));
//        System.out.println(new Solution().maximumDetonation(InputUtil.toDoubleIntegerArray("[[1,2,3],[2,3,1],[3,4,2],[4,5,3],[5,6,4]]")));
        System.out.println(new Solution().maximumDetonation(InputUtil.toDoubleIntegerArray("[[855,82,158],[17,719,430],[90,756,164],[376,17,340],[691,636,152],[565,776,5],[464,154,271],[53,361,162],[278,609,82],[202,927,219],[542,865,377],[330,402,270],[720,199,10],[986,697,443],[471,296,69],[393,81,404],[127,405,177]]")));
    }

    public static void main(String[] args) throws Exception {
        LC2101 an = new LC2101();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
